package com.my.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.entity.Token;
import com.my.mapper.TokenMapper;
import com.my.service.TokenService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TokenServiceImpl extends ServiceImpl<TokenMapper, Token> implements TokenService {

    @Override
    public boolean createToken(String token, Long userId, Integer type) {
        return baseMapper.insert(new Token().setToken(token).setUserId(userId).setType(type).setInvalidTime(DateUtil.offsetHour(new Date(), 2))) > 0;
    }

    @Override
    public void cleanObsoleteToken() {
        baseMapper.delete(new QueryWrapper<Token>().le("invalid_time", new Date()));
    }

    @Override
    public void updateToken(String token) {
        baseMapper.updateById(new Token().setToken(token).setInvalidTime(DateUtil.offsetHour(new Date(), 2)));
    }

}
